#include <panel.h>
#include "buffer.h"
#include "level.h"
#include "extras.h"

extern Level Map;
extern WINDOW* msgwin;
extern PANEL* msgpan;

void Buffer::Rec(string in){
	char* temp = (char *)in.c_str();
	if(temp[0]>='a' && temp[0]<='z'){
		temp[0] = (temp[0]-32);	//caps
		in = temp;
	}
	str = str + in;
	while(str.length() > maxlength){
		bool done=false;
		for(int i=maxlength-7;i>=0 && !done;--i){
			if(str.substr(i,1)==" "){
				str2 = str.substr(i+1,999);
				str = str.substr(0,i+1);
				done=true;
			}
		}
		Print();
	}
}
void Buffer::Now(string in){
	wmove(msgwin,0,0);
	for(int j=maxlength+8;j>0;--j){
		waddch(msgwin,' ');
	}
	wmove(msgwin,0,0);
	wprintw(msgwin,(char *)in.c_str());
//	wnoutrefresh(msgwin);
	top_panel(msgpan);
	update_panels();
	doupdate();
}
void Buffer::Print(bool auto_more){
	if(str!=""){
		int last = current-1;
		if(last==-1){
			last=19;
		}
		string temp = printed[last];
		string count="1";
		int pos = temp.find(" (x");
		if(pos!=string::npos){
			count = temp.substr(pos+3,100);
			count = count.substr(0,count.length()-1);
			temp = temp.substr(0,pos+1);
		}
		if(temp==str){
			printed[last] = temp + "(x" + IntToString(atoi(count.c_str())+1) + ")";
		}
		else{
			printed[current]=str;
			++current;
			if(current==20){
				current=0;
			}
		}
	}
	if(str2=="" && !auto_more){
		wmove(msgwin,0,0);
		for(int j=maxlength+8;j>0;--j){
			waddch(msgwin,' ');
		}
		wmove(msgwin,0,0);
		wprintw(msgwin,(char *)str.c_str());
//		wnoutrefresh(msgwin);
		update_panels();
		str="";
	}
	else{
		if(auto_more && str.length() > 59){ //i may have lost the newest code for this - the code that handled strings of any length.
			bool done=false;		//it's the same thing, but it loops until the string is short enough.
			string temp;
			for(int i=58;i>=0 && !done;--i){
				if(str.substr(i,1)==" "){
					temp = str.substr(0,i+1);
					str = str.substr(i+1,maxlength);
					done=true;
				}
			}
			wmove(msgwin,0,0);
			for(int j=maxlength+8;j>0;--j){
				waddch(msgwin,' ');
			}
			wmove(msgwin,0,0);
			wprintw(msgwin,(char *)temp.c_str());
			wattron(msgwin,COLOR_PAIR(COLOR_YELLOW));
			wprintw(msgwin,"[more]");
			wattroff(msgwin,COLOR_PAIR(COLOR_YELLOW));
			Map.Draw(); //check this at some point
//			wnoutrefresh(msgwin);
			top_panel(msgpan);
			update_panels(); //?
			doupdate();
			getch();
		}
		wmove(msgwin,0,0);
		for(int j=maxlength+8;j>0;--j){
			waddch(msgwin,' ');
		}
		wmove(msgwin,0,0);
		wprintw(msgwin,(char *)str.c_str());
		wattron(msgwin,COLOR_PAIR(COLOR_YELLOW));
		wprintw(msgwin,"[more]");
		wattroff(msgwin,COLOR_PAIR(COLOR_YELLOW));
		str=str2;
		str2="";
		Map.Draw(); //make sure these 2 parts are correct
		top_panel(msgpan);
		update_panels();
		doupdate();
		getch();
	}
}

